package com.acwing.lc.part1;

/**
 * @author rkc
 * @date 2022/9/14 16:15
 */
public class LC43字符串相乘 {

    public String multiply(String num1, String num2) {
        int[] l1 = new int[num1.length()], l2 = new int[num2.length()];
        for (int i = num1.length() - 1, j = 0; i >= 0; i--) l1[j++] = num1.charAt(i) - '0';
        for (int i = num2.length() - 1, j = 0; i >= 0; i--) l2[j++] = num2.charAt(i) - '0';
        int[] res = new int[num1.length() + num2.length()];
        for (int i = 0; i < l1.length; i++) {
            for (int j = 0; j < l2.length; j++) {
                res[i + j] += l1[i] * l2[j];
            }
        }
        for (int i = 0, t = 0; i < res.length; i++) {
            t += res[i];
            res[i] = t % 10;
            t /= 10;
        }
        StringBuilder sb = new StringBuilder();
        int k = res.length - 1;
        while (k > 0 && res[k] == 0) k--;
        while (k >= 0) sb.append(res[k--]);
        return sb.toString();
    }
}
